from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier

from sklearn.model_selection import GridSearchCV

def knn_gscv_iris():
    """
    用KNN算法会鸢尾花进行分类，添加网格搜索和交叉验证
    :return:
    """
    # 1.获取数据
    iris = load_iris()
    print(iris)
    # 2.划分数据集
    x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=6)
    print(x_train)
    print(x_test)
    print(y_train)
    print(y_test)
    # 3.特征工程：标准化
    transfer = StandardScaler()
    x_train = transfer.fit_transform(x_train)
    x_test = transfer.transform(x_test) #测试集使用训练集的数据（平均值）等进行标准化
    # 4.KNN 算法预估器
    estimator = KNeighborsClassifier()

    # 4.1 加入网格搜索与交叉验证
    param_dict = {"n_neighbors": [1, 3, 5, 7, 9, 11]}
    estimator = GridSearchCV(estimator,param_grid=param_dict, cv=10)

    estimator.fit(x_train, y_train)
    # 5.模型评估
    # 方法一：直接比对预测值与真实值
    y_predict = estimator.predict(x_test)
    print("y_predict:\n", y_predict)
    print("y_test:\n", y_test)
    print("预测值与真实值比对：\n",  y_predict == y_test)
    # 方法二：计算准确率
    score = estimator.score(x_test, y_test)
    print("准确率为：\n", score)

    # 最佳参数 best_params_
    print("最佳参数：\n", estimator.best_params_)
    # 最佳结果 best_score_
    print("最佳结果：\n", estimator.best_score_)
    # 最佳估计器 best_estimator_
    print("最佳估计器：\n", estimator.best_estimator_)
    # 交叉验证结果 cv_results_
    print("交叉验证结果：\n", estimator.cv_results_)

    return None

if __name__ == "__main__":
    # 代码12：用KNN算法会鸢尾花进行分类，添加网格搜索和交叉验证
    knn_iris_gscv()